package 题目集.动态规划.最大累加和;

import org.junit.Test;

/**
 * https://leetcode.cn/problems/maximum-product-subarray/description/
 */
public class ch02_乘积最大子数组 {
    public int maxProduct(int[] nums) {
        int res=nums[0];
        int preMax=nums[0];
        int preMin=nums[0];
        for (int i = 1; i <nums.length; i++) {
            int curMax=Math.max(nums[i],Math.max(preMax*nums[i],preMin*nums[i]));
            int curMin=Math.min(nums[i],Math.min(preMax*nums[i],preMin*nums[i]));
            res=Math.max(curMax,res);
            preMax=curMax;
            preMin=curMin;
        }
        return res;
    }
    @Test
    public void test(){
        int[] nums={-2,3,-4};
        int res = maxProduct(nums);
        System.out.println(res);
    }
}
